package jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 根据用户名，修改用户的密码，昵称，年龄
 * 程序启动后，要求用户输入用户名，密码，昵称，年龄
 * 然后完成需求
 */
public class PWDDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("修改用户数据");
        System.out.println("请输入用户名:");
        String username = scan.nextLine();
        System.out.println("请输入修改后的密码:");
        String password = scan.nextLine();
        System.out.println("请输入修改后的昵称:");
        String nickname = scan.nextLine();
        System.out.println("请输入修改后的年龄:");
        int age = Integer.parseInt(scan.nextLine());
        System.out.println(username+","+password+","+nickname+","+age);
        try (
                Connection connection = DBUtil.getConnection();
                ){
            Statement statement = connection.createStatement();
            String sql = "UPDATE user "+
                         "SET password='"+password+"',nickname='"+nickname+"',age="+age+" " +
                         "WHERE username='"+username+"'";
            System.out.println(sql);
            int num = statement.executeUpdate(sql);
            System.out.println(num>0?"修改成功":"修改失败");
        } catch (SQLException e) {
            e.getStackTrace();
        }


    }
}